﻿//-----------------------------------------------------------------------
// <copyright file="AuthorizationServiceBase.Operations.cs" company="Alan Rodrigues">
//     Copyright (c) Alan Rodrigues. All rights reserved.
// </copyright>
// <developed>Alan Rodrigues (operninha@gmail.com)</developed>
//-----------------------------------------------------------------------

namespace Authorization
{
    using System;
    using Properties;

    public partial class AuthorizationServiceBase
    {
        #region Operation

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public Scope GetScope(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            Application application = AuthorizationStoreManager.GetApplication(applicationName);
            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.GetScope", Resources.invalid_application);
            }
            return application.GetScope(scopeName);
        }

        public OperationCollection GetAllOperations(string applicationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            Application application = AuthorizationStoreManager.GetApplication(applicationName);
            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.GetAllOperations", Resources.invalid_application);
            }
            return application.GetOperations();
        }

        /// <summary>
        /// Returns all Operation Names
        /// </summary>
        /// <param name="applicationName"><see cref="Application"/> name</param>
        /// <returns>Returns a </returns>
        public string[] GetAllOperationNames(string applicationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            Application app = AuthorizationStoreManager.GetApplication(applicationName);
            if (app == null)
            {
                throw new AuthorizationException("AuthorizationService.GetAllOperationNames",
                                                 Resources.invalid_application);
            }
            return app.GetOperationNames();
        }
        public Operation GetOperation(string applicationName, string operationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");


            Application app = AuthorizationStoreManager.GetApplication(applicationName);
            if (app == null)
            {
                throw new AuthorizationException("AuthorizationService.GetOperation", Resources.invalid_application);
            }
            return app.GetOperation(operationName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="operationName"></param>
        /// <param name="description"></param>
        /// <param name="operationId"></param>
        /// <returns></returns>
        /// [OperationBehavior(Impersonation=ImpersonationOption.Required)]
        /// 
        public Operation CreateOperation(string applicationName, string operationName, string description, int operationId)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.CreateOperation", Resources.invalid_application);
            }

            Operation op = application.CreateOperation(operationId, operationName, description);
            op.Save();
            return op;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="operationName"></param>
        public void DeleteOperation(string applicationName, string operationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");

            Application application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.DeleteOperation", Resources.invalid_application);
            }
            application.DeleteOperation(operationName);
            application.Save();
        }

        public void UpdateOperation(string applicationName, string operationName, string newOperationName, string newOperationDescription, int newOperationId)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");

            Application application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.UpdateOperation", Resources.invalid_application);
            }
            Operation operation = application.UpdateOperation(operationName, newOperationName,
                                                              newOperationDescription, newOperationId);
            if (operation != null)
            {
                operation.Save();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="operationName"></param>
        /// <param name="newOperation"></param>
        /// <returns></returns>
        public void UpdateOperation(string applicationName, string operationName, Operation newOperation)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");

            if (newOperation == null)
            {
                throw new ArgumentNullException("newOperation");
            }

            this.UpdateOperation(applicationName, operationName, newOperation.Name, newOperation.Description, newOperation.OperationId);
        }

        public bool OperationExists(string applicationName, string operationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.OperationExists", Resources.invalid_application);
            }
            return (application.GetOperation(operationName) != null);
        }

        #endregion
    }
}
